<template>
	<view class="box">
		<view class="title">
			<view class="">你好， </view>
			<view class="">欢迎使用兴一报价</view>
		</view>
		<view class="shuruk">
			<view class="flexs">
				<image src="../../static/d3.png" mode=""></image>
				<input type="text" placeholder="账号" v-model="params.username" />
			</view>
			<view class="flexs">
				<image src="../../static/d2.png" mode=""></image>
				<view class="inputflex">
					<input v-model="params.password" :password="isshow" placeholder="密码" />
					<view @click="isshow =!isshow"
						style="width: 80rpx;display: flex;justify-content: center;align-items: center;">
						<uni-icons :type="isshow?'eye-slash' : 'eye'" size="24"></uni-icons>
					</view>
				</view>
			</view>
			<view class="flexs">
				<image src="../../static/d2.png" mode=""></image>
				<input type="number" confirm-type='done' placeholder="验证码" v-model="params.code" />
				<image class="code" :src="params.codeurl" mode="" @click="getcode()"></image>
			</view>
		</view>
		<view class="btn" @click="login">
			登录
		</view>
	</view>
</template>

<script>
	import {
		getlogin,
		getText,
		getUserinfo
	} from '@/common/http.api.js';
	export default {
		data() {
			return {
				isshow: true,

				account: '',
				password: '',
				code: '',
				codeimg: '',
				uuid: '',

				params: {
					codeurl: '',
					username: '',
					password: '',
					code: '',
					uuid: ''
				}
			};
		},
		onLoad() {
			this.getcode()
		},
		methods: {
			getcode() {
				getText().then(res => {
					this.params.codeurl = "data:image/gif;base64," + res.img;
					this.params.uuid = res.uuid
				})
			},
			login() {
				if (this.params.username == '') {
					uni.showToast({
						title: '请输入账号',
						duration: 2000,
						mask: false,
						icon: "none"
					})
				} else if (this.params.password == '') {
					uni.showToast({
						title: '请输入密码',
						duration: 2000,
						mask: false,
						icon: "none"
					})

				} else if (this.params.code == '') {
					uni.showToast({
						title: '请输入验证码',
						duration: 2000,
						mask: false,
						icon: "none"
					})
				} else {
					getlogin(this.params).then(res => {
						if (res.code == 200) {
							uni.setStorageSync('token', res.token)
							getUserinfo().then(res => {
								if (res.code == 200) {
									uni.setStorageSync('user', res.user)
									uni.redirectTo({
										url: '/pages/index/index'
									})
								} else {
									uni.showToast({
										title: '账号密码错误',
										duration: 2000,
										mask: false,
										icon: "none"
									})
								}
							})
						} else {
							this.getcode()
						}
					})
				}

			}
		}
	};
</script>

<style lang="scss">
	page {
		background: linear-gradient(180deg, #B3CBFF 0%, #FFFFFF 100%);
	}

	.box {
		padding: 0 34rpx;
	}

	.title {
		margin-top: 256rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: bold;
		font-size: 48rpx;
		color: #000000;
		line-height: 62rpx;
	}

	.code {
		width: 148rpx !important;
		height: 78rpx !important;
		border-radius: 10rpx;
	}

	input {
		width: 66%;
		font-size: 28rpx;
	}

	.btn {
		width: 680rpx;
		height: 90rpx;
		background: linear-gradient(90deg, #7EC9FF 0%, #4D5BFF 100%);
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		font-family: PingFang SC;
		font-weight: bold;
		font-size: 32rpx;
		color: #fff;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 200rpx;
	}

	.shuruk {
		z-index: 2;
		margin-top: 120rpx;

		.flexs {
			width: 680rpx;
			height: 110rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			display: flex;
			align-items: center;
			padding: 30rpx;
			margin-bottom: 43rpx;
			box-sizing: border-box;
		}

		.inputflex {
			width: 100%;
			display: flex;
			justify-content: space-between;
			align-items: center;

			input {
				flex: 1;
				font-size: 28rpx;
			}
		}

		image {
			width: 48rpx;
			height: 48rpx;
			margin-right: 10rpx;
		}

	}
</style>